//
//  BaseTabBarControllerViewController.swift
//  JiaXiaoTong
//
//  Created by 沈增光 on 2018/2/6.
//  Copyright © 2018年 IT. All rights reserved.
//

import UIKit

class BaseTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
     // self.tabBar.tintColor = [UIColor colorWithHexString:@"#76dcff"];
        // Do any additional setup after loading the view.
        
        self.tabBar.tintColor = UIColor_Hex.withHex(hexString: "#76dcff")
        self.addChildVc()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func addChildVc() {
        
        
        let index = NSObject.tp_integer(forKey: "teacherIndex")
        let resource = ((index ) == 0) ? "MainVCSettings" :"SutdentMainVc"
        let path  = Bundle.main.path(forResource: resource, ofType: "json")
        let data  =  NSData(contentsOfFile: path!)
        
        do{
           
            let dictArr = try JSONSerialization.jsonObject(with: data! as Data, options:JSONSerialization.ReadingOptions.mutableContainers) as! [NSDictionary]
            for   dict   in dictArr  {
            
                let vcName = dict["vcName"] as! String
                let title = dict["title"] as! String
                let imageName = dict["imageName"] as! String
                let selectedImageName = dict["selectedImageName"] as! String
                print(vcName)
                self.addChildViewController(childController: vcName, title: title, imageName: imageName, selectedImageName: selectedImageName)
                
               
            }
            
        }catch  {
            print("读取本地数据出现错误！")
        }
        
        
        
    }
    
    func addChildViewController (childController:String,title:String,imageName:String,selectedImageName:String){
      
        let module = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
       
     
        let a = module + "." + childController
       
        let conversionBase  =  NSClassFromString(a) as!  UIViewController.Type
      
            let baseController  = conversionBase.init()
            baseController.tabBarItem.image = UIImage.init(named: imageName)
            baseController.tabBarItem.selectedImage = UIImage.init(named: selectedImageName)
            // baseController.title =
            let nav  = BaseNavigationController.init(rootViewController: baseController)
            self.addChildViewController(nav)
        
        
       // UIViewController *base=[NSClassFromString(childController) new];
        //    base.tabBarItem.image=[UIImage imageNamed:imageName];
        //    base.tabBarItem.selectedImage=[UIImage imageNamed:selectedImageName];
        //    base.title = getLanguageManageKey(title);
        //    BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:base];
        //    [self addChildViewController:nav];
        
    }
        //:(NSString *) title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
    

    
//        -(void)addChildVC
//    {
//    NSInteger index = [NSObject tp_integerForKey:@"teacherIndex"];
//    NSString *resource = (index == 0) ? @"MainVCSettings" : @"SutdentMainVc";
//    NSString *path=[[NSBundle mainBundle] pathForResource:resource ofType:@"json"];
//    NSData *data=[NSData dataWithContentsOfFile:path];
//    NSArray *dictArr=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//    for (NSDictionary *dict in dictArr) {
//    [self addChildViewController:dict[@"vcName"] title:dict[@"title"] imageName:dict[@"imageName"] selectedImageName:dict[@"selectedImageName"]];
//    }
//    }
//    -(void)addChildViewController:(NSString *)childController title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
//    {
//
//    UIViewController *base=[NSClassFromString(childController) new];
//    base.tabBarItem.image=[UIImage imageNamed:imageName];
//    base.tabBarItem.selectedImage=[UIImage imageNamed:selectedImageName];
//    base.title = getLanguageManageKey(title);
//    BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:base];
//    [self addChildViewController:nav];
//    }
//
//    /**
//     *   shouldAutorotate  supportedInterfaceOrientations调用顺序最底层容器控制器有优先权级别，先判断tabbarController,再判断navgationController，在具体的某个contoller
//     *
//     */
    override var shouldAutorotate: Bool{
        
        let index = NSObject.tp_object(forKey: "teacherIndex")
        guard index as! Int  == 0 else
           {

            if  (self.selectedViewController?.isEqual(self.viewControllers?[0]))!{
                return (self.selectedViewController?.shouldAutorotate)!

                }
             return false
            }
//        if (self.selectedViewController?.isEqual(self.viewControllers?[1]))!{
//
//            return (self.selectedViewController?.shouldAutorotate)!
//        }
        return false
    }
//    - (BOOL)shouldAutorotate{
//    NSInteger index = [NSObject tp_integerForKey:@"teacherIndex"];
//    if (index == 0) {
//    if ([self.selectedViewController isEqual:[self.viewControllers objectAtIndex:1]]) {
//    return [self.selectedViewController shouldAutorotate];
//    }
//    }else{
//    if ([self.selectedViewController isEqual:[self.viewControllers objectAtIndex:0]]) {
//    return [self.selectedViewController shouldAutorotate];
//    }
//    }
//    return NO; // tabbar第一栏旋转控制交给下级控制器，其他栏不支持自动旋转
//    }
//
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        let index = NSObject.tp_integer(forKey: "teacherIndex")
   
        if (index == 0 ){

//            if (self.selectedViewController?.isEqual(self.viewControllers?[1]))!{
//            return (self.selectedViewController?.supportedInterfaceOrientations)!
                 //       }
        }else{
            if (self.selectedViewController?.isEqual(self.viewControllers?[0]))!{

        return (self.selectedViewController?.supportedInterfaceOrientations)!
                            }

        }
        return UIInterfaceOrientationMask.portrait
//        guard index as! Int == 0 else {
//            if (self.selectedViewController?.isEqual(self.viewControllers?[0]))!{
//
//                return (self.selectedViewController?.supportedInterfaceOrientations)!
//            }
//
//            return UIInterfaceOrientationMask.portrait
//        }
//        if (self.selectedViewController?.isEqual(self.viewControllers?[1]))!{
//
//            return (self.selectedViewController?.supportedInterfaceOrientations)!
//        }
       // return UIInterfaceOrientationMask.portrait
    }
//    - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//    NSInteger index = [NSObject tp_integerForKey:@"teacherIndex"];
//
//    if (index == 0) {
//    if ([self.selectedViewController isEqual:[self.viewControllers objectAtIndex:1]]) {
//    return [self.selectedViewController supportedInterfaceOrientations];
//    }
//    }else{
//    if ([self.selectedViewController isEqual:[self.viewControllers objectAtIndex:0]]) {
//    return [self.selectedViewController supportedInterfaceOrientations];
//    }
//    }
//    return UIInterfaceOrientationMaskPortrait; // tabbar第一栏控制器所支持旋转方向交给下级控制器处理，其他栏只支持竖屏方向
//    }
//

}
